﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN07_4A : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {   Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.AddClientScript();
            this.ViewMenu();            
            this.SetErrorMessage();
            this.SetDefault();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsolutePath));
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN07_1S.aspx");
    }
    
    #endregion
    #region "---------- Methods -----------"
    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }
    private void BindData(int ID)
    {
        List<GSBBondPrice> gSBBondPrice = new List<GSBBondPrice>();
        DataTable dt = new SPR_GSBBondPrice().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, gSBBondPrice);

            txtAllowedReturnDate.Text = DateTime.Now.DateToString(); // Default ให้เป็นวันที่ปัจจุบัน กรณีมีข้อมูลมาจากหน้าค้นหา
            txtGSBBondCode.Text = gSBBondPrice[0].GSBBondCode;            
            txtAllowedReturnPrice.Text = gSBBondPrice[0].AllowedReturnPrice.ToString();
            txtStatus.Text = "สร้างรายการ";                              

            hidGSBBondPriceID.Value = gSBBondPrice[0].GSBBondPriceID.ToString();
            hidUpdateDate.Value = gSBBondPrice[0].UpdatedDate.ToString();           
        }
    }
    private void SetDefault()
    {
        txtStatus.Text = "สร้างรายการ";
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqAllowedReturnDate.SetRequiredField(lblAllowedReturnDate, txtAllowedReturnDate, btnSAVE);
        reqGSBBondCode.SetRequiredField(lblGSBBondCode, txtGSBBondCode, btnSAVE);        
        reqAllowedReturnPrice.SetRequiredField(lblAllowedReturnPrice, txtAllowedReturnPrice, btnSAVE);              
    }
    private void SaveData(bool SaveNew)
    {
        try
        {
            GSBBondPrice gSBBondPrice = new GSBBondPrice();

            gSBBondPrice.GSBBondCode = txtGSBBondCode.Text;
            gSBBondPrice.AllowedReturnDate = txtAllowedReturnDate.Text.StringToDateAllowNull();              
            gSBBondPrice.AllowedReturnPrice = txtAllowedReturnPrice.Text.AsDecimal().Value;
            gSBBondPrice.IsConfirmed = false;
            gSBBondPrice.SetStandardField();
          
            int iID = new SPR_GSBBondPrice().INSERT(gSBBondPrice);

            if (iID > 0)
            {
                if (SaveNew)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                }
                else
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN07_2E.aspx?&id={0}", iID)));
                }
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN07_4A;
    }
    #endregion
}